import com.alibaba.druid.pool.DruidAbstractDataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.sql.Connection;
import java.sql.SQLException;
//
public class DruidDemo1 {
    public static <SQLException> void main(String[] args) throws java.sql.SQLException {
        //数据源初始化
        DruidDataSource DruidDataSource = new DruidDataSource();
        DruidAbstractDataSource dataSource = null;
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("050206yrx");
        dataSource.setInitialSize(5);//初始化连接池数量
        dataSource.setMaxActive(10);//最大连接池数量
        dataSource.setMaxWait(6000);//最大等待时间
        dataSource.setMinIdle(2);//最小连接池数量
        dataSource.setValidationQuery("select 1");//验证连接是否可用
        dataSource.setTestOnBorrow(true);//在取连接的时候验证
        dataSource.setTestOnReturn(true);//在还连接的时候验证
        dataSource.setTestWhileIdle(true);//在空闲时验证
        dataSource.setTimeBetweenEvictionRunsMillis(3000);//验证连接间隔时间
        dataSource.setMinEvictableIdleTimeMillis(60000);//最小空闲时间
        dataSource.setPoolPreparedStatements(true);//启用预编译
        dataSource.setMaxOpenPreparedStatements(20);//最大预编译
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);//每个连接最大预编译
        dataSource.setFilters("stat");//监控
        dataSource.setLogAbandoned(true);//监控
        dataSource.setRemoveAbandoned(true);//监控
        dataSource.setRemoveAbandonedTimeout(60);//监控

        DruidPooledConnection connection= (DruidPooledConnection) dataSource.getConnection();
        if(connection!=null){
            System.out.println("连接成功");
        }
    }
}
